package cn.yu.takeoutbackend.scientificResearchManagementSystem.service.impl;

import cn.yu.takeoutbackend.scientificResearchManagementSystem.dao.ScientificResearchManagementSystemMapper;
import cn.yu.takeoutbackend.scientificResearchManagementSystem.entity.ScientificStatisticsDocument;
import cn.yu.takeoutbackend.scientificResearchManagementSystem.service.ScientificResearchManagementSystemService;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Service()
@Slf4j
public class ScientificResearchManagementSystemServiceImpl implements ScientificResearchManagementSystemService {

   @Autowired
   private ScientificResearchManagementSystemMapper mapper;
   private List<ScientificStatisticsDocument> cache = new ArrayList<>();

    @Override
    public void upload(InputStream is) {
        final List<ScientificStatisticsDocument> list = EasyExcel.read(is).head(ScientificStatisticsDocument.class).sheet().doReadSync();
        for (ScientificStatisticsDocument document : list) {
            if(cache.size()<500){
                cache.add(document);
            }else {
                mapper.insertBatch(cache);
                cache.clear();
            }
            mapper.insertBatch(cache);
            cache.clear();
        }
    }

    @Override
    public List<ScientificStatisticsDocument> getAll() {
        final QueryWrapper<ScientificStatisticsDocument> wrapper = new QueryWrapper<>();
        final List<ScientificStatisticsDocument> list = mapper.selectList(wrapper);
        return list;
    }

    @Override
    public ScientificStatisticsDocument getOneById(int id) {
        return mapper.selectById(id);
    }
}
